package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.entity.ShoppingCart;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
 @Autowired
 private ShoppingCartMapper shoppingCartMapper;
    //加入购物车
public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
    //设置用户id，指定当前是哪个用户的数据
    Long id=BaseContext.getCurrent ();
    shoppingCart.setUserId (id);
    LambdaQueryWrapper<ShoppingCart> qw=new LambdaQueryWrapper<> ();
    //比较用户id
    qw.eq (ShoppingCart::getUserId,id);
    ShoppingCart selectone=shoppingCartMapper.selectOne (qw);
    if(selectone!=null){
        //存在，数量加一
        Integer number=selectone.getNumber ();
        if(number==1){
            delete ();
            //return R.success (selectone);
        }
        selectone.setNumber (number-1);
        shoppingCartMapper.updateById (selectone);
    }
    return R.success (selectone);
}
//查看购物车
    public R<List<ShoppingCart>>select(){
        LambdaQueryWrapper<ShoppingCart> qw=new LambdaQueryWrapper<> ();
        qw.eq (ShoppingCart::getUserId,BaseContext.getCurrent ());
        qw.orderByDesc (ShoppingCart::getCreateTime);
        List<ShoppingCart> list=shoppingCartMapper.selectList (qw);
        return R.success (list);
    }
    //清空购物车
    public R<String>delete(){
        LambdaQueryWrapper<ShoppingCart> qw=new LambdaQueryWrapper<> ();
        qw.eq (ShoppingCart::getUserId,BaseContext.getCurrent ());
        shoppingCartMapper.delete (qw);
        return R.success ("清除成功");
    }
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //设置用户id，指定当前是哪个用户的数据
        Long id=BaseContext.getCurrent ();
        shoppingCart.setUserId (id);
        //查询当前菜品或套餐是否在购物车中
        Long dishId=shoppingCart.getDishId ();
        LambdaQueryWrapper<ShoppingCart> qw=new LambdaQueryWrapper<> ();
        //比较用户id
        qw.eq (ShoppingCart::getUserId,id);
        if(dishId!=null){
            //加入菜品
            qw.eq (ShoppingCart::getDishId,dishId);

        }else {
            //加入套餐
            qw.eq (ShoppingCart::getSetmealId,shoppingCart.getSetmealId ());
        }
        ShoppingCart selectone=shoppingCartMapper.selectOne (qw);
        if(selectone!=null){
            //存在，数量加一
            Integer number=selectone.getNumber ();
            selectone.setNumber (number+1);
            shoppingCartMapper.updateById (selectone);
        }else {
            shoppingCart.setNumber (1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert (shoppingCart);
            selectone=shoppingCart;
            //不存在添加到购物车，数量为1
        }
        return R.success (selectone);
    }
}
